package com.sunchen.asc.application.aspect;

import com.alibaba.fastjson.JSON;
import cn.hutool.json.JSONObject;
import com.sunchen.asc.service.log.api.LogAspectService;
import com.sunchen.asc.service.log.dto.LogDto;
import org.apache.dubbo.config.annotation.DubboReference;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Map;
import java.util.logging.Logger;

@Aspect
@Component
@Order(5)
public class LogAspect {

    @DubboReference
    LogAspectService logAspectService;


    private Logger logger = Logger.getLogger(getClass().toString());

    @Pointcut("execution(public * com.sunchen.asc.application.rest..*.*(..))")
    public void log(){}

    @Before("log()")
    public void doBefore(JoinPoint joinPoint) throws Throwable{
        // 接收到请求，记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        // 记录下请求内容
        //请求地址
        logger.info("URL : " + request.getRequestURL().toString());
        //GET，POST
        logger.info("HTTP_METHOD : " + request.getMethod());
        //ip
        logger.info("IP : " + request.getRemoteAddr());
        //操作接口
        logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());

        logger.info("ARGS : " + Arrays.toString(joinPoint.getArgs()));
        //浏览器
        logger.info("user-agent"+request.getHeader("User-Agent"));

        logger.info("params:"+request.getParameterMap());
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());

        String user = request.getHeader("user");
        JSONObject userJsonObject = new JSONObject(user);
        String id = userJsonObject.get("id").toString();

        logger.info("userId"+id);

        LogDto logDto = new LogDto();
        logDto.setTerminal("");
        logDto.setUser(id);
        logDto.setCreateTime(new Timestamp(System.currentTimeMillis()));
        Map<String,String[]> paramap = request.getParameterMap();
        String json = JSON.toJSONString(paramap);
        logDto.setDetail("METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()+",PARAMS : "+json);
        logDto.setIp(request.getRemoteAddr());
        logDto.setBrowser(request.getHeader("User-Agent"));
        logAspectService.addLog(logDto);

    }

//    @AfterReturning(returning = "ret", pointcut = "log()")
//    public void doAfterReturning(Object ret) throws Throwable {
//        // 处理完请求，返回内容
//        logger.info("RESPONSE : " + ret);
//    }
}
